package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gxuwz.medical.dao.Family_memberDAO;
import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.family_member.Family_member;
import com.gxuwz.medical.domain.ill_card.Ill_card;

public class Ill_cardServle extends BaseServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m = request.getParameter("m");// 动作类型参数
		if ("list".equals(m)) {
			process(request, response, "/page/ill_card/ill_card_list.jsp");
		} else if ("findandinput".equals(m)) {
			process(request, response, "/page/ill_card/ill-nh_addList.jsp");
		} else if ("get".equals(m)) {
			process(request, response, "/page/ill_card/ill_card_edit.jsp");
		} else if ("add".equals(m)) {
			try {
				add(request, response);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			process(request, response, "/page/ill_card/ill_card_list.jsp");
		} else if ("edit".equals(m)) {
			this.edit(request, response);
		} else if ("del".equals(m)) {
			this.del(request, response);
		} else if ("find".equals(m)) {
			this.find(request, response);
		} else if ("input".equals(m)) {
			this.operadd(request, response);
			process(request, response, "/page/ill_card/ill_card_add.jsp");
		}
	}

	private void operadd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String IDcard = request.getParameter("IDcard");
		System.out.println("IDcard==" + IDcard);
		String join_nh_code = request.getParameter("join_nh_code");
		System.out.println("join_nh_code==" + join_nh_code);
		String membername = request.getParameter("membername");
		System.out.println("membername==" + membername);
	}

	/**
	 * 
	 * @Title: find @Description: 查询添加用户 @author 陈震洺 @param req @param resp @throws
	 *         ServletException @throws IOException @throws
	 */
	private void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("来到了find()");
		// 1：接收参数
		String IDcard = req.getParameter("IDcard");
		System.out.println("IDcard==" + IDcard);
		// 2:构造新慢病信息对象
		Family_memberDAO model = new Family_memberDAO();
		List<Family_member> list = new ArrayList<Family_member>();

		try {

			List<Family_member> list1 = model.findByKeyValue("IDcard", IDcard);

			req.setAttribute("list", list1);

			System.out.println(list1);
			process(req, resp, "/page/ill_card/ill-nh_addList.jsp");
		} catch (Exception e) {
			error(req, resp);
		}

	}

	private void add(HttpServletRequest rs, HttpServletResponse response)
			throws ServletException, IOException, ParseException {
		System.out.println("来到了添加方法（）");
		// 接收页面传递过来的参数
		String ill_card_no = rs.getParameter("ill_card_no");
		String join_nh_code = rs.getParameter("join_nh_code");
		String IDcard = rs.getParameter("IDcard");
		String ill_code = rs.getParameter("ill_name");
		String start_day1 = rs.getParameter("start_day");
		String end_day1 = rs.getParameter("end_day");
		Timestamp start_day = null;
		Timestamp end_day = null;
		start_day = Timestamp.valueOf(start_day1);
		end_day = Timestamp.valueOf(end_day1);
		String areacode = rs.getParameter("areacode");
		String user_id = rs.getParameter("user_id");

		HttpSession session = rs.getSession();
		areacode = session.getAttribute("agencode").toString();
		// 实例化Family类
		Ill_card ill_card = new Ill_card();
		// 调用添加方法
		try {
			Area area = new Area(areacode);
			ill_card.addIll_card(ill_card_no, join_nh_code, IDcard, ill_code, area, start_day, end_day, user_id);
		} catch (Exception e) {
			error(rs, response);
		}

	}

	private void edit(HttpServletRequest rs, HttpServletResponse resp) throws ServletException, IOException {

		// 1：接收参数
		String ill_card_no = rs.getParameter("ill_card_no");

		String ill_name = rs.getParameter("ill_name");

		try {
			// 2:构造新慢病信息对象
			Ill_card model = new Ill_card(ill_card_no, ill_name);
			// 3：调用保存的方法
			model.edit();
			process(rs, resp, "/page/ill_card/ill_card_list.jsp");
		} catch (Exception e) {
			error(rs, resp);
		}

	}

	private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 1：接收参数
		String ill_card_no = req.getParameter("id");

		// 2:构造新慢病信息对象
		Ill_card model = new Ill_card();
		try {
			model.del(ill_card_no);
			process(req, resp, "/page/ill_card/ill_card_list.jsp");
		} catch (Exception e) {
			error(req, resp);
		}

	}

}
